package phase3;
import java.util.Vector;
public class Block{
	private char char1=' ';
	private char char2=' ';
	private char char3=' ';
	private int numberOfChar;
	private int id;
	public static int idCount=0;
	private Vector <Block> previous =new Vector <Block>();
	private Vector <Block> next=new Vector <Block>();
	
	//method to assign an id to the block
	private void setId(){
		id=idCount++;
	}
	//the method to find space in a block and return the index
	private int findSpace()
	{
		if(char1==' ')
			return 0;
		else if(char2==' ')
			return 1;
		else if(char3==' ')
			return 2;	
		else
			return -1;
	}
	//Okay
	//the method to get the number of characters in a block
	public int getNumOfChar()
	{
		int numOfChar=0;
		if(char1!=' ')
			numOfChar++;
		if(char2!=' ')
			numOfChar++;
		if(char3!=' ')
			numOfChar++;
		return numOfChar;	
	}
	//default constructor
	public Block()
	{
		char1=' ';
		char2=' ';
		char3=' ';	
		numberOfChar=0;
		setId();
		//previous=NULL;
		//next=NULL;
	}
	//parameter constructor
	public Block(char char1,char char2,char char3)
	{
		this.char1=char1;
		this.char2=char2;
		this.char3=char3;	
		this.numberOfChar=this.getNumOfChar();
		setId();
		//previous=NULL;
		//next=NULL;
	}
	//Okay
	//the method to change the character whern there is an old character at that index.Otherwise, you cannot do this
	public boolean chChar(int index,char newCh)
	{
		if(index==0)
		{
			if(char1!=' ')
			{	
				char1=newCh;
				return true;
			}
		}
		else if(index==1)
		{
			if(char2!=' ')
			{	
				char2=newCh;
				return true;
			}				
		}
		else if(index==2)
		{
			if(char3!=' ')
			{	
				char3=newCh;
				return true;
			}		
		}
		return false;
	}
	//Okay
	//the method to add character to the block when there is space
	public boolean addChar(char ch)
	{
		int space=this.findSpace();
		if(space==0)
		{
			char1 = ch;
			return true;
		}
		else if(space==1)
		{
			char2 = ch;
			return true;
		}
		else if(space==2)
		{
			char3 = ch;
			return true;
		}
		else
			return false;
	}	
	//Okay
	//the method to access the characters in the block
	public char getChar(int index)
	{
		if(index==0)
			return char1;
		else if(index==1)
			return char2;
		else if(index==2)
			return char3;
		else
			return '?';	
	}
	//The method that can get the vector of the previous blocks
	public Vector <Block> getPreviousBlock()
	{	
		return previous;		
	} 
	//The method that can get the vector of the next blocks
	public Vector <Block> getNextBlock()
	{
		return next;	
	}
	//The method that can set the previous block to the vector
	public void setPreviousBlock(Block b)
	{
		this.previous.add(b);
	}
	//The method that can set the next block to the vector
	public void setNextBlock(Block b)
	{
		this.next.add(b);
	}
	
	public String toString(){
		String temp="Block "+(char)('A'+id)+" :[";
		for (int i = 0,count=0; count < getNumOfChar(); i++,count++){
			if (getChar(i) != ' '){
				temp+= getChar(i);
				if (count != getNumOfChar()-1) 
					temp+= " ";
			}
			else
				count--;
		}
		temp+="]";
		if(next.size()!=0){
			temp+="\tpoint to: ";
			for(int i=0;i<next.size();i++){
				temp+=(char)(next.get(i).id+'A')+" ";
			}
		}
		return temp;
	}


}